package game.module.nn.processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.club.logic.ClubRoomManager;
import game.module.hall.logic.CheatManager;
import game.module.nn.bean.DdzRoomCfg;
import game.module.nn.bean.NnJindouCfg;
import game.module.nn.bean.NnRoom;
import game.module.nn.dao.DdzRoomTemplateCache;
import game.module.nn.dao.NnJindouTemplateCache;
import game.module.nn.logic.NnDataManager;
import game.module.nn.logic.NnLogic;
import game.module.sss.bean.SssJindouCfg;
import game.module.sss.dao.SssJindouTemplateCache;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageNn.C2SNNCreateRoom;
import lua.LuaMessageNn.S2CNNCreateRoom;

@MsgCodeAnn(msgcode = C2SNNCreateRoom.id, accessLimit = 500)
public class NnCreateRoomProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(NnCreateRoomProcessor.class);

	@Override
	public void process(PlayingRole hero, RequestMessageRaw request) throws Exception {
		C2SNNCreateRoom reqMsg = C2SNNCreateRoom.parse(request);
		int cfg_index = reqMsg.cfg_index;
		byte roomFeeType = reqMsg.roomFeeType;
		byte is_jindou = reqMsg.is_jindou;
		byte jindou_type = reqMsg.jindou_type;
		int jindou = reqMsg.jindouNum;
		int playerId = hero.getId();
		int realJindou = 0;
		logger.info("nn create room,playeyId={},cfg_index={}", playerId, cfg_index);
		// 是否在房间中
		boolean isInRoom = NnDataManager.getInstance().isPlayerInRoom(playerId);
		if (isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(102);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// is fangka enough
		// int costFangka = 10;
		// if (hero.getPlayerBean().getFang_ka() < costFangka) {
		// S2ErrorCode respMsg = new S2ErrorCode(103);
		// hero.writeAndFlush(respMsg.build(hero.alloc()));
		// return;
		// }
		DdzRoomCfg ddzRoomCfg = DdzRoomTemplateCache.getInstance().getTemplate(cfg_index);
		int costFangka = ddzRoomCfg.getCost_fangka();
		if (hero.getPlayerBean().getFang_ka() < costFangka) {
			S2ErrorCode respMsg = new S2ErrorCode(103);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		if(is_jindou ==1 ) {//金豆房间
			if (hero.getPlayerBean().getClub_id() == 0) {// 没有俱乐部
				S2ErrorCode respMsg = new S2ErrorCode(134);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
			NnJindouCfg ratecfg = NnJindouTemplateCache.getInstance().getTemplate(0);
			if (jindou_type != 3) {// 0 1 2 3 下标
				NnJindouCfg ssscfg = NnJindouTemplateCache.getInstance().getTemplate(jindou_type);
				realJindou = ssscfg.getJindou();
				logger.info("nn={}", ssscfg.getJindou());
			} else {
				realJindou = jindou;
			}
			if (hero.getPlayerBean().getJin_dou() < realJindou * ddzRoomCfg.getRound() * ratecfg.getAdmittanceRate()) {
				S2ErrorCode respMsg = new S2ErrorCode(132);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
		}
		// do
		NnRoom nnRoom = NnLogic.getInstance().createNnRoom(playerId, ddzRoomCfg.getRound());
		nnRoom.setRoomFeeType(roomFeeType);
		nnRoom.setRoomfee(costFangka);
		nnRoom.setPrevent_cheat(reqMsg.prevent_cheat);
		nnRoom.setJindou(realJindou);
		nnRoom.setIs_jindou(is_jindou);
		CheatManager.getInstance().openCheatComm(nnRoom.getPrevent_cheat(), nnRoom.getIps(), hero);
		ClubRoomManager.getInstance().openClubRoom(hero, nnRoom);

		S2CNNCreateRoom respMsg = new S2CNNCreateRoom();
		respMsg.room_id = nnRoom.getUuid();
		respMsg.round_count = nnRoom.getRoundCount();
		respMsg.roomFeeType = roomFeeType;
		respMsg.is_jindou = is_jindou;
		reqMsg.jindouNum = realJindou;
		// ret
		hero.writeAndFlush(respMsg.build(hero.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// null
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// null
	}

}
